package kr.co.onc.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class BaseInterceptor extends HandlerInterceptorAdapter {
	private String menuCode="";
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		
		
		StringBuffer paramSb = null;
		Enumeration eNames = request.getParameterNames ();
		if ( eNames.hasMoreElements () ) {
			paramSb = new StringBuffer ();
			int paramCount = 0;
			
			while ( eNames.hasMoreElements () ) {
				paramCount += 1; // 파라미터 개수
				
				String name = (String) eNames.nextElement ();
				String[] values = request.getParameterValues (name);
				if (name.equals("menuCode")) {
					menuCode=values[0];
				}
				if ( values.length > 0 ) {
					String value = values[0];
					
					for ( int i = 1; i < values.length; i++ ) {
						value += "," + values[i];
					}
					if ( paramCount > 1 ) {
						paramSb.append (", ");
					}
					paramSb.append (name + "=" + value);
				}
			}
		}
		
		
		
		
		request.setAttribute ("queryString", paramSb); // 리소스 경로
		
		return super.preHandle(request, response, handler);
	}
	
	
	
	@Override
	public void afterCompletion(
			HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
			
		request.setAttribute("menuCode",menuCode);
		
	}
	
	

}
